var config = require('../../utils/config.js');
var util = require('../../utils/util.js');
var liveAPI = require('../../utils/live.js');
var demandTools = require('../../utils/demand.js');
var API_URL = config.API_URL;
var app = getApp()
var getData = that => {
    util.POST(API_URL.index, {})
        .then(res => {
            that.setData(res.data.data);
           my.stopPullDownRefresh();
        }).catch(res => {
           my.stopPullDownRefresh();
            console.log(res);
        })
}
Page({
    data: {
        showRecommend: false,
        indicatorDots: true,
        autoplay: true,
        interval: 5000,
        duration: 1000,
        imgUrls: [
            'http://img.jadelibrary.com/data/files/mall/ad/201711231526047097.png',
            'http://img.jadelibrary.com/data/files/mall/ad/201711231524493024.png',
            'http://img.jadelibrary.com/data/files/mall/ad/201711231521112734.png',
            'http://img.jadelibrary.com/data/files/mall/ad/201711231521412182.png',
            'http://img.jadelibrary.com/data/files/mall/ad/201711231520262580.png'
        ]
    },
    onLoad() {
        var that = this;
        my.httpRequest({
            url: 'https://v1.develop.yudu.docdic.com/api/home',
            dataType: 'json',
            methods: "get",
            success: function (res) {
                console.log(res.data.data);
                that.setData(res.data.data);
            },
            fail: function (res) {
                console.log(res)
            },
            complete: function (res) {
                my.hideLoading();
                console.log(res)
            }
        });
        let getLoginStatus = my.getStorageSync({ key: "isTGX" }).data;
        if (getLoginStatus) {
            that.setData({
                showRecommend: true
            })
        } else {
            that.setData({
                showRecommend: false
            })
        }
    },
    onPullDownRefresh: function () {
        getData(this);
    },
    // 直播间跳转
    goToLive: function () {

        my.navigateTo({
            url: '/pages/live/live',
        })
    },
    ad: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        console.log(index)
        // 拿到当前点击的图片及连接数据
        let ad = that.data.advertisement[index];
        console.log(ad);
        let url, action;
        let tag = Number(ad.second_type);
        console.log(tag)
        switch (tag) {
            // 4,5,6可以，其他的页面还需要写
            case 4: url = '/pages/mail/mail'; action = 's'; break;
            case 5: url = '/pages/live/live'; action = 's'; break;
            case 6: url = '/pages/demand/demand'; action = 's'; break;
            case 7: url = '/pages/good/good?id=' + ad.link_url; action = 'n'; break;
            case 8: url = '/pages/demand/info/info'; action = 'n'; app.globalData.demand = ad.demand; break;
            case 9: url = '/pages/mail/mail'; action = 'n'; break;
            case 10: url = '/pages/mail/mail'; action = 'n'; break;
            case 11: url = '/pages/article/news/news?typeId=0&id=' + ad.link_url; action = 'n'; break;
            default: url = ''; action = '';
        }
        console.log(url);
        if (action == 's') {
            my.navigateTo({
                url: url,
            })
        } else if (action == 'n') {
            my.navigateTo({
                url: url,
            })
        }
    },
    // 新闻列表跳转
    goToNews: function (e) {
        my.navigateTo({
            url: '/pages/article/news_list/news_list'
        })
    },
    // 新闻详情页面跳转
    news: function (e) {
        console.log(e);
        var id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: "/pages/article/news/news?id=" + id + '&typeId=0'
        })
    },
    // 求购页面跳转
    goToDemand: function () {
        my.switchTab({
            url: '/pages/demand/demand',
        })
    },
    // 求购详情页面跳转
    info: function (e) {
        console.log(e);
        let that = this;
        let index = e.currentTarget.dataset.index;
        console.log(index)
        let demands = that.data.demands;
        console.log(demands)
        if (demands[index].handleStatus != '未完成') {
            return
        }
        app.globalData.demand = demands[index];
        my.navigateTo({
            url: '/pages/demand/info/info',
        })
    },
    // 玉器商城跳转页
    goToMall: function () {
        my.switchTab({
            url: '/pages/mail/mail',
        })
    },
    // 玉器详情页跳转
    goToGood: function (e) {
        console.log(e);
        var id = e.currentTarget.dataset.id;
        my.navigateTo({
            url: '/pages/good/good?id=' + id
        })
    },
    // 玉器直播详情跳转
    live: function (e) {
        let that = this;
        let index = e.currentTarget.dataset.index;
        // 拿到点击的直播房间的信息 判断是否有绑定 没有就跳转绑定
        if (that.data.rooms[index].liveId == undefined) {
            demandTools.bindLive();
        } else {
            let live = that.data.rooms[index];
            console.log(live)
            // liveAPI.goToLive(live);
            my.showToast({
                content: '暂时还没有连接到直播间', // 文字内容
                duration: 2000
            });
        }
    },
    // 邀请
    addI: function (e) {
        let index = e.currentTarget.dataset.index;
        let demands = this.data.demands;
        demandTools.addI(demands[index].demandId);
    },
    // 推荐
    addR: function (e) {
        let index = e.currentTarget.dataset.index;
        let demands = this.data.demands;
        demandTools.addR(demands[index].demandId);
    },
});
